import delrawdataHtml from 'ngtemplate!html!../../../tpls/dialog/delrawdata.dialog.html';

class RawdataListController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, ScoreService, tableService, iRunner){

        var vm = this;

        vm.$rootScope    = $rootScope;
        vm.$cookieStore  = $cookieStore;
        vm.$timeout      = $timeout;
        vm.$state        = $state;
        vm.ngDialog      = ngDialog;
        vm.dialogService = dialogService;
        vm.ScoreService  = ScoreService;
        vm.tableService  = tableService;
        vm.iRunner       = iRunner;

        let record_id, type;

        // 初始化表单数据
        vm.access_code = '';

        // 初始化表格控件, ajax请求数据
        vm.dataTable=vm.tableService.getRawData(null, vm.$cookieStore.get('access_token'), vm.$state.params.race_id);

        // 清空原始数据
        vm.deleteAll = () => {
            vm.ngDialog.open({
                template: delrawdataHtml,
                className: 'ngdialog-theme-default ngdialog-alert',
                scope: $scope
            });
        }

        // 清空原始数据, 表单提交
        $rootScope.submitDelForm = (isValid, access_code) => {

            // 验证失败, 提示用户
            if (!isValid) {
                vm.dialogService.error('请输入密码');
            }

            // 验证通过
            if (isValid) {
                
                // 关闭模态框
                vm.ngDialog.close();

                // 清空选手
                vm.ScoreService.postRawdataClear({'access_code': access_code}, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                .then(function(data) {
                    
                    // 成功调用
                    data.code==0&&vm.dialogService.success('已清空原始数据');

                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error('密码错误');

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, function(data) {
                    console.log('error')
                });

            }

            return false;
        }

        /**
         * 导出原始数据
         * @return {[type]} [description]
         */
        vm.download = function() {
            //下载成绩链接
            //导出按钮 请求http://timing.com/dashboard/rawdata/download?race_id=当前赛事
            vm.window = window.open("/dashboard/rawdata/download?race_id=" + vm.$state.params.race_id + "&access-token=" + this.$cookieStore.get('access_token'));                 
        }

        // 设置无效
        angular.element(document).off('click').on('click', '[data-action="status"]', function(event) {
            
            event.stopPropagation();

            record_id = $(this).attr('data-id');
            type      = $(this).attr('data-type');

//            vm.ngDialog.open({
//                template: `
//                <form class="form-horizontal">
//                    <div class="form-group">
//                        <div class="col-lg-12">
//                            <p>是否继续操作</p>
//                        </div>
//                    </div>
//                    <div class="form-group">
//                        <div class="col-xs-6 text-left">
//                            <button type="button" class="btn btn-sm btn-block btn-default" ng-click="closeThisDialog()">取消</button>
//                        </div>
//                        <div class="col-xs-6 text-right">
//                            <button type="button" class="btn btn-sm btn-block btn-primary" ng-click="submit()">继续</button>
//                        </div>
//                    </div>
//                </form>`,
//                className: 'ngdialog-theme-default ngdialog-alert',
//                plain: true
//            });
            $rootScope.submit();
        });

        // 设置无效
        $rootScope.submit = () => {
//            vm.ngDialog.close();
            vm.ScoreService.postRawdataStatus({'type': type, 'record_id': record_id}, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then( data => {
                vm.dataTable.search();
                
                //成功调用
                //data.code==0 && vm.$state.reload();
                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error('稍等片刻');

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);

//                console.log(data)
            }, error => console.log('error'));
        }

    }

}

RawdataListController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'ScoreService', 'tableService', 'iRunner']; 

export default RawdataListController;